package com.quanyue.haircut.shop.service.impl;

import com.quanyue.haircut.domain.entity.AssessImage;
import com.quanyue.haircut.domain.entity.TechnicianAssess;
import com.quanyue.haircut.domain.entity.UserIntegral;
import com.quanyue.haircut.shop.dto.AssessDetail;
import com.quanyue.haircut.shop.dto.AssessRequest;
import com.quanyue.haircut.shop.mapper.AssessImageMapper;
import com.quanyue.haircut.shop.mapper.AssessMapper;
import com.quanyue.haircut.shop.service.AssessService;
import com.quanyue.haircut.shop.service.IntegralService;
import com.quanyue.haircut.shop.service.TechnicianService;
import com.vhuomai.mybatis.mapper.page.Page;
import com.vhuomai.mybatis.mapper.page.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.ArrayList;

@Service
public class AssessServiceImpl implements AssessService {
    @Autowired
    private AssessMapper assessMapper;
    @Autowired
    private TechnicianService technicianService;
    @Autowired
    private AssessImageMapper assessImageMapper;
    @Autowired
    private IntegralService integralService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createAssess(AssessRequest assessRequest, TechnicianAssess technicianAssess) {
        technicianAssess.setCreateTime(LocalDateTime.now());
        technicianAssess.setShopId(technicianService.selectTechnician(assessRequest.getTechnicianId()).getTechnician().getShopId());
        assessMapper.insert(technicianAssess);
        this.insertImage(technicianAssess.getId(), assessRequest.getImages());
        // 更新会员积分
        UserIntegral userIntegral = integralService.findIntegral(technicianAssess.getUserId(), technicianAssess.getShopId());
        BigInteger integer = BigInteger.valueOf(10);
        if (userIntegral != null) integer = userIntegral.getIntegral().add(integer);
        integralService.updateIntegral(technicianAssess.getUserId(), technicianAssess.getShopId(), integer);
    }


    @Override
    public Page <AssessDetail> findAssessList(PageRequest request, Integer technicianId) {
        Page <AssessDetail> detailPage = assessMapper.findAssessDetail(request, technicianId);
        detailPage.getContent().forEach(assessDetail -> {
            assessDetail.setImages(assessImageMapper.findByAccessId(assessDetail.getId()));
        });
        return detailPage;
    }

    private void insertImage(Integer assessId, ArrayList <String> images) {
        ArrayList <AssessImage> assessImages = new ArrayList <>(images.size());
        images.forEach(image -> {
            AssessImage assessImage = new AssessImage();
            assessImage.setAssessId(assessId);
            assessImage.setCreateTime(LocalDateTime.now());
            assessImage.setImage(image);
            assessImages.add(assessImage);
        });
        assessImageMapper.insertAll(assessImages);
    }
}
